package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaUtil;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.ShowLargeImageActivity;
import com.hemaapp.yjnh.bean.ImageItem;
import com.hemaapp.yjnh.bean.Img;
import com.hemaapp.yjnh.bean.ImgItemsBean;
import com.hemaapp.yjnh.bean.Reply;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.listener.ReplyItemClickListener;
import com.hemaapp.yjnh.utils.ImageUtils;
import com.hemaapp.yjnh.view.ImageGridView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 唠嗑，帖子详情
 *
 * @author ZhangGuiyou
 *         Date: 2015-08-19 13:31
 */
public class LaokeReplyAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Reply> mDatas;
    private ReplyItemClickListener itemClickListener;

    public LaokeReplyAdapter(Context mContext, ArrayList<Reply> replies) {
        super(mContext);
        this.mContext = mContext;
        this.mDatas = replies;
    }

    public void setItemClickListener(ReplyItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getCount() {
        int size = mDatas == null ? 0 : mDatas.size();
        return size == 0 ? 1 : size;
    }

    @Override
    public boolean isEmpty() {
        int size = mDatas == null ? 0 : mDatas.size();
        return size == 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null || convertView.getTag(R.id.TAG_REPLY) == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_laoke_reply, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_REPLY, holder);
        } else
            holder = (ViewHolder) convertView.getTag(R.id.TAG_REPLY);
        holder.setDatas(position);
        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.iv_avatar)
        ImageView ivAvatar;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.tv_delete)
        TextView tvDelete;
        @Bind(R.id.iv_reply)
        ImageView ivReply;
        @Bind(R.id.tv_reply)
        TextView tvReply;
        @Bind(R.id.image_grid)
        ImageGridView imageGrid;
        int position = -1;
        AddImageAdapter imageAdapter;
        ArrayList<ImgItemsBean> replyImgs = new ArrayList<>();

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            ivAvatar.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (position != -1) {
                        ArrayList<Img> imgs = new ArrayList<Img>();
                        String imgurl_detail = mDatas.get(position).getAvatar();
                        Img img = new Img(imgurl_detail);
                        imgs.add(img);

                        Intent intent = new Intent();
                        intent.setClass(mContext, ShowLargeImageActivity.class);
                        intent.putExtra("imgs", imgs);
                        intent.putExtra("position", 1);
                        mContext.startActivity(intent);
                    }
                }
            });
            ivReply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.onReplyClckListener(position);
                }
            });
            tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.onDeleteClckListener(position);
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            Reply reply = mDatas.get(position);
            ImageUtils.loadAvatar(mContext, reply.getAvatar(), ivAvatar);
            tvName.setText(reply.getNickname());
            tvDate.setText(HemaUtil.transTimeChat(reply.getRegdate()));
            //显示帖子内容，并将其中emoji表情进行转换
            HemaUtil.SetMessageTextView(mContext, tvReply, reply.getContent());
            //显示评论的删除按钮
            User user = BaseApplication.getInstance().getUser();
            if (user != null && user.getId().equals(reply.getClient_id())) {
                tvDelete.setVisibility(View.VISIBLE);
            } else {
                tvDelete.setVisibility(View.GONE);
            }
            replyImgs.clear();
            replyImgs.addAll(reply.getItems());
            refreshImages();
        }
        private void refreshImages() {
            if (imageAdapter == null) {
                imageAdapter = new AddImageAdapter(mContext, replyImgs);
                imageGrid.setAdapter(imageAdapter);
                imageAdapter.setShowDelete(false);
                imageAdapter.setShowAdd(false);
                imageAdapter.setImageClickListener(new AddImageAdapter.OnImageClickListener() {
                    @Override
                    public void onImageClickListener(int position) {
                        ArrayList<Img> imgs = new ArrayList<Img>();
                        for (int i = 0; i < replyImgs.size(); i++) {
                            ImgItemsBean item = replyImgs.get(i);
                            String imgurl_detail = item.getImgurlbig();
                            Img img = new Img(imgurl_detail);
                            imgs.add(img);
                        }

                        Intent intent = new Intent();
                        intent.setClass(mContext, ShowLargeImageActivity.class);
                        intent.putExtra("imgs", imgs);
                        intent.putExtra("position", position + 1);
                        mContext.startActivity(intent);
                    }

                    @Override
                    public void onDeleteClickListener(int position, ImgItemsBean imageItem) {
                    }
                });
            } else {
                imageAdapter.notifyDataSetChanged();
            }
        }
    }
}
